package cfgs.gestudent.finestra.assignatura;

import java.awt.Color;
import java.awt.Insets;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import java.awt.GridBagLayout;

import javax.swing.JTable;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import cfgs.gestudent.dao.assignatures.EliminarAssignatura;
import cfgs.gestudent.dao.assignatures.MostrarAssignatura;
import cfgs.gestudent.dao.assignatures.TaulaControladoraModificacionsAssignatura;
import cfgs.gestudent.preferencies.ConfiguracionsIdiomes;
import cfgs.gestudent.preferencies.HintTextField;
import cfgs.gestudent.preferencies.PreferenciesAnyAcademic;

import java.awt.GridBagConstraints;

import javax.swing.JTextField;
import javax.swing.JPopupMenu;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javax.swing.JMenuItem;
import javax.swing.JLabel;

public class FinestraMostrarAssignatura extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5976525230072735029L;
	
	private JTable table;
	private JScrollPane scpTable;
	private DefaultTableModel dtmEjemplo;
	private MostrarAssignatura ma = null;
	private JTextField txtCerca;
	private JPopupMenu popupMenu;
	private JMenuItem mntmBorrar;
	
	private ConfiguracionsIdiomes idioma = new ConfiguracionsIdiomes();
	private ResourceBundle rb = idioma.getIdioma();
	
	private ArrayList<Integer> idAssign;
	private int row;
	private JLabel lblCercar;
	
	private String sql;
	/**
	 * Create the panel.
	 */
	public FinestraMostrarAssignatura() {
		
		setBackground(Color.WHITE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {0, 30, 30, 30, 30, 30, 0, 0};
		gridBagLayout.rowHeights = new int[] {30, 30};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{Double.MIN_VALUE, 1.0};
		setLayout(gridBagLayout);
		
		table = new JTable();
		GridBagConstraints gbc_table = new GridBagConstraints();
		gbc_table.fill = GridBagConstraints.BOTH;
		gbc_table.gridwidth = 7;
		gbc_table.gridx = 0;
		gbc_table.gridy = 1;
		ma = new MostrarAssignatura();
		
//		row = table.getSelectedRow();
		
		//rb.getString("hintextFieldAssign")
		txtCerca = new JTextField();
		txtCerca.getDocument().addDocumentListener(new DocumentListener() {
			
			
			
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				ma = new MostrarAssignatura();
				sql = "SELECT a.Nom, a.Credits, t.Nom, a.Id AS id FROM ASSIGNATURA a, TIPUS_OBLIGACIO t WHERE a.Id_Tipus_obligacio=t.Id AND a.Nom LIKE '%"+txtCerca.getText()+"%' GROUP BY a.id";
				ma.setSql(sql);
				dtmEjemplo = new DefaultTableModel(ma.getFilas(), ma.getColumnas()){
					@Override
				    public boolean isCellEditable (int row, int column)
				    {
				        if (column == 0)
				        	return true;
						if (column == 1)
				           return true;
				        			
				        return false;
				    }
				};
				table.setModel(dtmEjemplo);
				table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacionsAssignatura(ma.getFilas(), "Nom", 3));
				table.getColumnModel().getColumn(1).setCellEditor(new TaulaControladoraModificacionsAssignatura(ma.getFilas(), "Credits", 3));
				
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				ma = new MostrarAssignatura();
				sql = "SELECT a.Nom, a.Credits, t.Nom, a.Id AS id FROM ASSIGNATURA a, TIPUS_OBLIGACIO t WHERE a.Id_Tipus_obligacio=t.Id AND a.Nom LIKE '%"+txtCerca.getText()+"%' GROUP BY a.id";
				ma.setSql(sql);
				dtmEjemplo = new DefaultTableModel(ma.getFilas(), ma.getColumnas()){
					@Override
				    public boolean isCellEditable (int row, int column)
				    {
						if (column == 0)
				        	return true;
						if (column == 1)
				           return true;
				        			
				        return false;
				    }
				};
				table.setModel(dtmEjemplo);
				table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacionsAssignatura(ma.getFilas(), "Nom", 3));
				table.getColumnModel().getColumn(1).setCellEditor(new TaulaControladoraModificacionsAssignatura(ma.getFilas(), "Credits", 3));
				
				
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				ma = new MostrarAssignatura();
				sql = "SELECT a.Nom, a.Credits, t.Nom, a.Id AS id FROM ASSIGNATURA a, TIPUS_OBLIGACIO t WHERE a.Id_Tipus_obligacio=t.Id AND a.Nom LIKE '%"+txtCerca.getText()+"%' GROUP BY a.id";
				ma.setSql(sql);
				dtmEjemplo = new DefaultTableModel(ma.getFilas(), ma.getColumnas()){
					@Override
				    public boolean isCellEditable (int row, int column)
				    {
						if (column == 0)
				        	return true;
						if (column == 1)
				           return true;
				        			
				        return false;
				    }
				};
				table.setModel(dtmEjemplo);
				table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacionsAssignatura(ma.getFilas(), "Nom", 3));
				table.getColumnModel().getColumn(1).setCellEditor(new TaulaControladoraModificacionsAssignatura(ma.getFilas(), "Credits", 3));
				
			}
		});
		
		lblCercar = new JLabel(rb.getString("lblCercar"));
		GridBagConstraints gbc_lblCercar = new GridBagConstraints();
		gbc_lblCercar.insets = new Insets(0, 0, 5, 5);
		gbc_lblCercar.anchor = GridBagConstraints.EAST;
		gbc_lblCercar.gridx = 4;
		gbc_lblCercar.gridy = 0;
		add(lblCercar, gbc_lblCercar);
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 5;
		gbc_textField.gridy = 0;
		add(txtCerca, gbc_textField);
		txtCerca.setColumns(10);
		
		
		scpTable = new JScrollPane();
		
		scpTable.setViewportView(table);
		add(scpTable, gbc_table);
		table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		
		sql = "SELECT a.Nom, a.Credits, t.Nom, a.Id AS id FROM ASSIGNATURA a, TIPUS_OBLIGACIO t WHERE a.Id_Tipus_obligacio=t.Id GROUP BY a.id";
		
		ma.setSql(sql);
		dtmEjemplo = new DefaultTableModel(ma.getFilas(), ma.getColumnas()){
			@Override
		    public boolean isCellEditable (int row, int column)
		    {
				if (column == 0)
		        	return true;
				if (column == 1)
		           return true;
		        			
		        			
		        return false;
		    }
		};
		
		table.setModel(dtmEjemplo);
		
		table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacionsAssignatura(ma.getFilas(), "Nom", 3));
		table.getColumnModel().getColumn(1).setCellEditor(new TaulaControladoraModificacionsAssignatura(ma.getFilas(), "Credits", 3));
		
		
		
		
		
//		TableRowSorter<TableModel> sort = new TableRowSorter<TableModel>(dtmEjemplo);
//		table.setRowSorter(sort);
		
		popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);
		
		mntmBorrar = new JMenuItem(rb.getString("borrar"));
		mntmBorrar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ma = new MostrarAssignatura();
				int row = table.getSelectedRow();
				Object [][] obj = ma.getFilas();
				int idBorrar = (int) obj[row][3];
				EliminarAssignatura ea = new EliminarAssignatura();
				ea.eliminarRegistre(idBorrar, table.getValueAt(row, 0).toString());
				sql = "SELECT a.Nom, a.Credits, t.Nom, a.Id AS id FROM ASSIGNATURA a, TIPUS_OBLIGACIO t WHERE a.Id_Tipus_obligacio=t.Id GROUP BY a.id";
				ma.setSql(sql);
				dtmEjemplo = new DefaultTableModel(ma.getFilas(), ma.getColumnas()){
					@Override
				    public boolean isCellEditable (int row, int column)
				    {
				        if (column == 0)
				        	return true;
						if (column == 1)
				           return true;
				        			
				        return false;
				    }
				};
				table.setModel(dtmEjemplo);
				table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacionsAssignatura(ma.getFilas(), "Nom", 3));
				table.getColumnModel().getColumn(1).setCellEditor(new TaulaControladoraModificacionsAssignatura(ma.getFilas(), "Credits", 3));
			}
		});
//		popupMenu.add(mntmBorrar);
		
	}
	
	private int veureIdAnyAncademic(){
		int idAny = 0;
		
		PreferenciesAnyAcademic paa = new PreferenciesAnyAcademic();
		idAny = paa.getAnyAcademic();
		
		return idAny;
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
